<?php

require_once('./includes/phpunit_setup.inc');

class models_class_php_TestCase extends DrupalTddTestCase{

  function setup() {
    parent::setup();
    drupal_install_modules(array('kasahorow', 'kservices', 'devel', 'kentry'));
  }

  public function testEntryTemplateModel() {
    $tpl = new EntryTemplate('ak');
    $tpl->name = 'Akan';
    $tpl->editors = array('1', '2');
    $tpl->description = 'This is fun';
    $tpl->links = array('en', 'fr');
    $tpl->save();
    $this->assertTrue($tpl->saved, 'Template saving failed');

    $field = new EntryField('ak');
    $field->label = 'Label for something';
    $field->field_type = 'textfield';
    $field->save();

    $retrieved_tpl = new EntryTemplate('ak');
    $row = $retrieved_tpl->get();
    $this->assertTrue($row->created > 0, 'Creation timestamp not set');
    $this->assertEquals($tpl->id, $row->id, 'Template ID not saved');
    $this->assertEquals($tpl->name, $row->name, 'Name not saved');
    $this->assertEquals($tpl->description, $row->description, 
                        'Description not saved');
    $this->assertEquals($tpl->editors, $row->editors, 'Editors not saved');
    $this->assertEquals($tpl->links, $row->links, 'Links not saved');
    $this->assertEquals(array('title' => 'Title', 'body' => 'Body',
      'group' => 'Group', 'sample' => 'Sample',
      $field->field_id => 'TEXTFIELD: Label for something'), $row->fields,
      'Fields not saved');

  }

  public function testEntryFields() {
    $field = new EntryField('ak');
    $field->label = 'Label';
    $field->field_type = 'textfield';
    $field->save();
    $this->assertTrue($field->saved, 'Field saving failed');

    $retrieved_field = new EntryField('ak');
    $retrieved_field->field_id = $field->field_id;
    $row = $retrieved_field->get();
    $this->assertTrue($row->field_id > 0, 'Field ID not set');
    $this->assertEquals($field->label, $row->label, 'Label not saved');
    $this->assertEquals($field->field_type, $row->field_type,
                        'Field type not saved');
    $this->assertEquals($field->entry_type, $row->entry_type,
                        'Entry type not saved');
  }

  public function testEntryRelations() {
    $node1 = array('title' => 'Title', 'uid' => 45, 'status' => 1,
                   'type' => 'page');
    $node2 = array('title' => 'Title', 'uid' => 45, 'type' => 'story');
    $node1 = (object)$node1;
    $node2 = (object)$node2;
    node_save($node1); # Generate nid, vid
    node_save($node2);
    $rel = new EntryRelation($node1->nid,$node2->nid, 'syn');
    $rel->uid = $node1->uid;
    $rel->save();
    $this->assertTrue($rel->saved, 'Relation saving failed');
    $row = $rel->get();
    $this->assertTrue($row->updated > 0, 'Field update time not set');
    $this->assertEquals($node1->nid, $row->from_nid);
    $this->assertEquals($node2->nid, $row->to_nid);
    $this->assertEquals('syn', $row->relation_id);
    $this->assertEquals($rel->uid, $row->uid, 'User ID not set');
  }

  public function testEntryModel() {
    $entry = new Entry();
    $entry->author_id = 3;
    $fields = array('title' => 'This title', 'body' => 'Body of node',
                    'group' => 'Root', 'sample' => 'Ex', '11' => 'Field 11',
                    'pcat' => '44', 'media' => "a\nb\n");
    $entry->fields = $fields;
    $entry->type = 'en';
    $entry->save();
    $this->assertTrue($entry->saved, 'Entry saving failed');

    $row = new Entry($entry->entry_id);
    $row->get();
    $this->assertEquals($entry->author_id, $row->author_id, 'UID not set');
    $this->assertEquals(1, $row->status, 'Status defaults to 1');
    $this->assertEquals($fields, $row->fields, 'Fields not set');
    $this->assertEquals('en', $row->type, 'Type not set');

    # Now update some part of the node and resave
    $nentry = new Entry($entry->entry_id);
    $nentry->fields['title'] = $title = 'Changed title';
    $nentry->fields['sample'] = $example = 'Changed example';
    $nentry->save();
    $row = $nentry->get();
    $this->assertEquals($title, $row->fields['title'], 'Title not updated');
    $this->assertEquals($example, $row->fields['sample'], 'Example not updated');
    $this->assertEquals($nentry->author_id, $row->author_id,
                        'Should not change');
    $this->assertEquals(1, $row->status, 'Status defaults to 1');
    $this->assertEquals($fields['group'], $row->fields['group'],
                        'Group should not change');
    $this->assertEquals($fields['11'], $row->fields['11'],
                        'Field 11 should not change');
    $this->assertEquals($fields['body'], $row->fields['body'],
                        'Body should not change');
    $this->assertEquals('en', $row->type, 'Type not set');
  }
}
